//
//  NewLoanView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/9.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import SwiftyJSON

@objc protocol NewLoanViewDelegate {
    @objc optional func changeNewLoanViewFrame(count:Int) //改变坐标
    @objc optional func selectNews(model:NewLoanModel) //查看新闻
    @objc optional func moreNewLoan() //更多贷款资讯
}

class NewLoanView : UIView {
    
    weak var delegate : NewLoanViewDelegate?
    
    fileprivate let cellId = "newLoanCell"
    var dataArray = [NewLoanModel]()
    
    fileprivate var tableView : UITableView!

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false //禁止滑动
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(NewLoanCell.self, forCellReuseIdentifier: cellId)
        self.addSubview(tableView)
        
        let headView = NewLoanHeadView.init(frame: .init(x: 0, y: 0, width: ScreenW, height: RATIO_H(maxNum: 40)))
        headView.text = latestLoanTips
        headView.isFlag = true
        headView.moreButton.addTarget(self, action: #selector(moreAction), for: .touchUpInside)
        tableView.tableHeaderView = headView
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
    }
    
    //MARK: 更多贷款资讯
    @objc fileprivate func moreAction() {
        
        self.delegate?.moreNewLoan!()
    }
    
    //MARK: 获取数据
    public func getData() {
        
        KMTool.requestGetData(URLString: url_loanLatestInformation, parameters: ["psw":"kameng98_loan"], success: { (data) in
            
            let json = JSON.init(data)
            if let array = json.arrayObject {
                
                if array.count == 0 {
                    return
                }else{
                    self.dataArray.removeAll()
                }
                
                for item in array {
                    let model = NewLoanModel.setModel(with: item as! [String : Any])
                    self.dataArray.append(model)
                    
                    if self.dataArray.count == 5 {
                        break
                    }
                }
                
                self.tableView.reloadData()
            }
            
            self.delegate?.changeNewLoanViewFrame!(count: self.dataArray.count)
            
        }) {
            
            self.delegate?.changeNewLoanViewFrame!(count: self.dataArray.count)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension NewLoanView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 50)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NewLoanCell
        cell?.model = dataArray[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        self.delegate?.selectNews!(model: dataArray[indexPath.row])
    }
}
